
package com.tools.cleanmaster.ui.view.landingpage;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LayoutAnimationController;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.tools.cleanmaster.R;
import com.tools.cleanmaster.taskmain.ProcessItem;
import com.tools.cleanmaster.ui.card.animator.AnimationListenerAdapter;

import java.util.ArrayList;
import java.util.List;

public class LandingPageRunAppBackView extends LinearLayout {
    private static final int RUNAPP_ICON_VERT = 4;

    public interface ClearAppAnimListener {
        void onAnimationEnd();
    }

    class GridViewAdapter extends BaseAdapter {
        private Context mContext;
        private ArrayList<Drawable> mList;

        public GridViewAdapter(Context context, ArrayList<Drawable> list) {
            mContext = context;
            mList = list;
        }

        @Override
        public int getCount() {
            return mList.size();
        }

        @Override
        public Object getItem(int position) {
            return mList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView mImageView;
            if (convertView == null) {
                mImageView = new ImageView(mContext);
                mImageView.setLayoutParams(new GridView.LayoutParams(mIconSize, mIconSize));
            } else {
                mImageView = (ImageView) convertView;
            }
            mImageView.setImageDrawable(mList.get(position));
            return mImageView;
        }

    }

    private int mIconSize;

    private GridView mGridView;
    private LayoutAnimationController mFlipAnimationController;
    private Context mContext;

    public LandingPageRunAppBackView(Context ctx) {
        super(ctx);
        init();
    }

    public LandingPageRunAppBackView(Context ctx, AttributeSet attrs) {
        super(ctx, attrs);
        init();
    }

    private void init() {
        mContext = getContext();
        inflate(getContext(), R.layout.landing_page_apps_run_back_view_layout, this);
        mGridView = findViewById(R.id.backview_icon);
        mIconSize = getResources().getDimensionPixelOffset(R.dimen.runapp_iconshow_size);

        Animation flipAnimation = AnimationUtils.loadAnimation(mContext, R.anim.appicon_out_anim);
        flipAnimation.setFillAfter(true);
        mFlipAnimationController = new LayoutAnimationController(flipAnimation);
        mFlipAnimationController.setOrder(LayoutAnimationController.ORDER_NORMAL);
        mFlipAnimationController.setDelay(0.4f);
    }

    public void showRunApps(List<ProcessItem> needClearProcess) {
        Drawable drawable;
        ArrayList<Drawable> iconList = new ArrayList<Drawable>();
        for (int i = 0; i < needClearProcess.size(); i++) {
            try {
                drawable = needClearProcess.get(i).icon;
            } catch (Exception e) {
                drawable = mContext.getResources().getDrawable(
                        android.R.drawable.sym_def_app_icon);
            }
            iconList.add(drawable);
        }
        mGridView.setAdapter(new GridViewAdapter(mContext, iconList));
    }

    public void clearApps(final ClearAppAnimListener listener) {
        mGridView.setLayoutAnimation(mFlipAnimationController);
        mGridView.setLayoutAnimationListener(new AnimationListenerAdapter() {
            @Override
            public void onAnimationEnd(Animation animation) {
                if (listener != null) {
                    listener.onAnimationEnd();
                }
                mGridView.setLayoutAnimationListener(null);
            }

        });
        mGridView.post(new Runnable() {
            @Override
            public void run() {
                mGridView.startLayoutAnimation();
            }
        });
    }
}
